package com.steven.ourcontest.chat.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.chat.viewmodel.SelectFriendViewModel
import com.steven.ourcontest.chat.viewmodel.UserDetailViewModel
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.activity_new_friends.*
import kotlinx.android.synthetic.main.activity_new_friends.iv_icon
import kotlinx.android.synthetic.main.activity_new_friends.tv_name
import java.lang.Exception

class NewFriendsActivity : BaseActivity() {
    private val selectFriendViewModel: SelectFriendViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getViewId(): Int? {
        return R.layout.activity_new_friends
    }

    override fun initView() {
        iv_select?.setOnClickListener {
            try {
                val id = et_id?.text.toString().toLong()
                selectFriendViewModel.queryUserDetail(id)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        selectFriendViewModel.userDetailResponse.observe(this, Observer {
            if (it.data == null) {
                it.msg?.let { it1 -> toast(it1) }
                cl_friend?.isVisible = false
                return@Observer
            }
            cl_friend?.isVisible = true
            it?.data?.let { data ->
                tv_name?.text = data.uname
                data.iconUrl?.let { icon ->
                    Glide.with(this)
                        .load(NetConstants.ICON_BASE_URL + icon)
                        .error(R.drawable.ic_avatar)
                        .apply(
                            RequestOptions.bitmapTransform(
                                GlideRoundTransform(
                                    this,
                                    10
                                )
                            )
                        )
                        .into(iv_icon)
                }
                cl_friend?.setOnClickListener {
                    AppRouter.openUserDetailActivity(this, data.uid)
                }
            }
        })
    }
}